package com.gomtel.smartdevice.ui.activity.tools;

import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.moudle.db.entity.HeartRateDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.utils.ChartUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/31 0031.
 * 心率显示
 */

public class HeartRateActivity extends WatchAppBaseActivity implements View.OnClickListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.chart_line)
    LineChart chart_line;
    @BindView(R.id.center_hrv)
    TextView center_hrv;
    @BindView(R.id.min_hrv)
    TextView min_hrv;
    @BindView(R.id.max_hrv)
    TextView max_hrv;
    @BindView(R.id.bpm_content)
    TextView bpm_content;

    private ChartUtils chartUtils;
    private List<HeartRateDataInfo> heartRateDataInfos;
    private List<Integer> HeartRateList = new ArrayList<>();
    private int value = 0;
    private int size = 0;
    private int min = 0;
    private int max = 0;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        int code = commonevent.getCode();
        switch (code) {
            case EventCode.HEART_RATE:
                HeartRateList.clear();
                value = 0;
                size = 0;
                min = 0;
                max = 0;
                setContentView(getLayout());
                mUnBinder = ButterKnife.bind(this);
                initInject();
                initData();
                initView();
                initListener();
                Log.e(TAG, "registerEvent");
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_heart_rate;
    }

    @Override
    protected void initClass() {
        chartUtils = new ChartUtils();
    }

    @Override
    protected void initData() {
        heartRateDataInfos = dataManager.queryHeartRateDataList(UserContent.UserName);
        for (HeartRateDataInfo heartRateDataInfo : heartRateDataInfos) {
            HeartRateList.add(heartRateDataInfo.getHeratRate());
            value = value + heartRateDataInfo.getHeratRate();
        }
        if (HeartRateList.size() > 0) {
            min = Collections.min(HeartRateList);
            max = Collections.max(HeartRateList);
            size = HeartRateList.size();
        } else {
            size = 1;
        }
    }

    @Override
    protected void initView() {
        title_name.setText(getString(R.string.HeartRate));
        center_hrv.setText("平均心率\n" + value / size + "\nbpm");
        min_hrv.setText("最小心率\n" + min + "\nbpm");
        max_hrv.setText("最大心率\n" + max + "\nbpm");
        bpm_content.setText(value / size + "");
        chartUtils.setHeartLineChart(chart_line);
        loadLineChartData(chart_line);
    }

    @Override
    protected void initListener() {
        img_btn_black.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_btn_black:
                finish();
                break;
        }
    }

    private void loadLineChartData(final LineChart chart) {
        chart.setNoDataText("暂无数据");
        ArrayList<LineDataSet> allLinesList = new ArrayList<>();
        ArrayList<Entry> entryList = new ArrayList<>();
        for (int i = 0; i < HeartRateList.size(); i++) {
            //Entry(yValue,xIndex);一个Entry表示一个点，第一个参数为y值，第二个为X轴List的角标
            entryList.add(new Entry(HeartRateList.get(i), i));
        }
        //LineDataSet可以看做是一条线
        LineDataSet dataSet = new LineDataSet(entryList, "dataLine");
        dataSet.setLineWidth(0.5f);
        dataSet.setCircleSize(0f);// 显示的圆形大小
        dataSet.setDrawValues(false); // 隐藏折线图每个数据点的值
        dataSet.setValueTextColor(getResources().getColor(R.color.gray));
        dataSet.setDrawCubic(true);//允许设置平滑曲线
        dataSet.setCircleColor(getResources().getColor(R.color.gray));
        dataSet.setCircleColorHole(getResources().getColor(R.color.gray));
        dataSet.setColor(getResources().getColor(R.color.color_fatigue_red));
        dataSet.setDrawFilled(false);//是否填充折线下方
//        dataSet.setFillColor(Color.rgb(76, 219, 255));//折线图下方填充颜色设置
        dataSet.setFillAlpha(100);
        dataSet.setValueTextSize(24f);
        allLinesList.add(dataSet);
        LineData mChartData = new LineData(getXAxisShowLable(), allLinesList);
        chart.setData(mChartData);
        // 设置动画
        chart.setVisibleXRangeMaximum(entryList.size() + 1);
        chart.setVisibleXRangeMinimum(entryList.size() - 1);
    }

    public ArrayList<String> getXAxisShowLable() {
        ArrayList<String> m = new ArrayList<>();
        for (int i = 0; i < HeartRateList.size(); i++) {
            m.add(String.valueOf(i));
        }
        return m;
    }
}
